package it.lorenz.server.fk.wc;

import java.io.IOException;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;

public class WebCacheManager {

    private MemcachedClient memC = null;

    public WebCacheManager(String servers) throws IOException {
        memC = new MemcachedClient(AddrUtil.getAddresses(servers));
    }

    public void shutdown() {
        if (memC != null) {
            memC.shutdown();
        }
        memC = null;
    }

    public Object readCache(String key) {
        if (key == null) {
            throw new IllegalArgumentException("key null");
        }
        return memC.get(key);
    }

    public void writeCache(String key, int expirationTime, Object toCache) {
        if (key == null) {
            throw new IllegalArgumentException("key null");
        }
        if (toCache == null) {
            throw new IllegalArgumentException("toCache null");
        }
        memC.set(key, expirationTime, toCache);
    }
}
